/**
 * 
 */
package controller;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import exception.ApplicationException;
import service.SecurityService;
import service.SecurityServiceImpl;
import service.UserQueryHelper;
import service.UserService;
import service.UserServiceImpl;
import utils.Page;
import model.Door;
import model.Message;
import model.User;

/**
 * @ClassName: UserAction
 * @Description:
 * @author Hongming Wu
 * @date 2013-12-4
 * 
 */
public class UserAction implements SessionAware {
	private Map<String, Object> session;
	private User user;
	private String birth;
	private String type;
	private Door door;
	private List<Door> doorList;
	private Message msg;
	private UserQueryHelper helper = null;
	private Page page = null;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Door getDoor() {
		return door;
	}

	public void setDoor(Door door) {
		this.door = door;
	}

	public List<Door> getDoorList() {
		return doorList;
	}

	public void setDoorList(List<Door> doorList) {
		this.doorList = doorList;
	}

	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public UserQueryHelper getHelper() {
		return helper;
	}

	public void setHelper(UserQueryHelper helper) {
		this.helper = helper;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;

	}

	public String gotoUser() {
		return "success";
	}

	public String home() {
		user = (User) session.get("loginedUser");
		return "homePage";
	}

	public String update() {
		UserService us = new UserServiceImpl();
		try {
			us.updateUser(user);
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
		System.out.println("update successful!");
		user = (User) session.get("loginedUser");
		return "success";
	}

	public String gotoFireAlarm() {
		SecurityService sas = new SecurityServiceImpl();
		doorList = sas.loadAllDoor();
		user = (User) session.get("loginedUser");
		return "firealarmPage";
	}

	public String loadMyLog() {

		if (page == null) {
			page = new Page();

		}
		SecurityService sas = new SecurityServiceImpl();
		UserService us = new UserServiceImpl();
		doorList = sas.loadAllDoor();
		user = (User) session.get("loginedUser");
		page = us.loadPagedLogs(page, helper, user);

		return "loadPagedLogPage";
	}

	public String createFireMsg() {
		msg = new Message();
		msg.setMsg_Type("Fire");
		String content = door.getDoor_ID() + " : Fire !!!";
		msg.setMsg_Content(content);
		user = (User) session.get("loginedUser");
		UserService us = new UserServiceImpl();
		us.createMsg(user, msg);
		return "success";
	}
	
	public String gotoErrorReport() {
		SecurityService sas = new SecurityServiceImpl();
		doorList = sas.loadAllDoor();
		user = (User) session.get("loginedUser");
		return "errorReportPage";
	}
	
	public String createErrorMsg() {

		msg.setMsg_Type("Error");
		msg.setMsg_Content(door.getDoor_ID() +" : "+ msg.getMsg_Content());
		user = (User) session.get("loginedUser");
		UserService us = new UserServiceImpl();
		us.createMsg(user, msg);
		return "success";
	}
	
	public String gotoMyAccess(){
		return "myAccessPage";
	}
	
	public String openDoor(){
		UserService us = new UserServiceImpl();
		user = (User) session.get("loginedUser");
		try{
			us.openDoor(door,user);
		}catch (ApplicationException e) {
			e.printStackTrace();
		}
		System.out.println("open successful!");
		return "success";
	}
}
